# -*- coding: utf-8 -*-


from openerp import models, fields, api,osv
from openerp.tools.translate import _
import openerp.addons.decimal_precision as dp
from datetime import datetime
from openerp.exceptions import ValidationError

class assign_order(models.TransientModel):
	_name = 'assign.order'
	_description = 'Picking wizard'
	user_id = fields.Many2one('res.users',u'门店',domain=[('is_stores','=',True)])
	
	@api.one
	def assign(self):
		for apply_obj in self.pool['apply.delivery'].browse(self._cr, self._uid,self._context.get('active_ids'),context=self._context):
			if self.user_id.id:
				
				apply_obj.message_subscribe_users([self.user_id.id])
				apply_obj.message_post(u"总部分配给你一份下的订单",subtype='mail.mt_comment') #总部分配订单给门店的时候门店收到邮件提示
			if apply_obj.is_shop_accept:
				raise ValidationError(u'卡号为%s的券门店已接受' % apply_obj.copy_code) #当卡券已经分配过给门店在此分配会报错
				#raise Exception(u'卡号为%s的券已发货'%apply_obj.copy_code)
		self.pool['apply.delivery'].write(self._cr, self._uid, self._context.get('active_ids'),{'assigned_shop_id':self.user_id.id},context=self._context)

class assign_accept(models.TransientModel):
	_name = 'assign.accept'
 	_description = u'门店批量接收总部分配的提货记录'
		
	@api.model
	def default_total(self):
		confirm_msg = u'批量接受总部分配的提货记录,共' + str(len(self._context.get('active_ids'))) + u'条'#批量接受数量总计
		return confirm_msg
	
	message = fields.Char(default=default_total,readonly=True)
	
	def accept_button(self,cr,uid,ids,context=None):
		'''批量接受总部分配向导'''
		self.pool['apply.delivery'].accept_button(cr,uid,context.get('active_ids'),context=context)
		
